// store/index.ts
import { defineStore } from 'pinia'
// 1. 定义容器、导出容器
// 参数1：容器的ID，必须是唯一的，后面Pinia会把所有的容器挂载到根容器
// 参数2：一些选项对象，也就是state、getter和action
// 返回值：一个函数，调用即可得到容器实例

export const useKeepAliveStore = defineStore('keepAlive', {
	state: () => {
		return {
			keepLiveRoute: [],
			routeKey: null,
			routeShow: true
		}
	},
	getters: {
		getRouteShow(): boolean {
			return this.routeShow;
		},
		getKeepLiveRoute() {
			return this.keepLiveRoute;
		}
	},
	actions: {
		pushKeepLive(component: any) {
			if (!this.keepLiveRoute.includes(component)) {
				this.keepLiveRoute.push(component)
			}
		},
		removeKeepLive(component: any) {
			var index = this.keepLiveRoute.indexOf(component);
			if (index !== -1) {
				this.keepLiveRoute.splice(index, 1);
			}
		},
		clearKeepLive() {
			this.keepLiveRoute = []
		},
		setRouteKey(key: any) {
			this.routeKey = key
		},
		setRouteShow(key: any) {
			this.routeShow = key
		}
	}
})

// 2. 使用容器中的 state
// 3. 通过 getter 修改 state
// 4. 使用容器中的 action 同步和异步请求